package com.jxt.myjxtmanager.db;

import com.jxt.myjxtmanager.db.dao.StudentDao;

import java.util.Collection;
import java.util.List;

import de.greenrobot.dao.query.LazyList;

/**
 * Created by qm on 2017/3/7 0007.
 */
public class StudentModel {

    private static StudentModel instance;
    private StudentDao studentDao;

    public synchronized static StudentModel getInstance() {
        if (instance == null) {
            instance = new StudentModel();
        }
        return instance;
    }

    private StudentModel() {
        studentDao = DBHelper.getInstance().getDaoSession()
                .getStudentDao();
    }

    public boolean insertOrReplace(Student message) {
        studentDao.insertOrReplace(message); // 30ms
        return true;
    }

    public boolean update(Student message) {
        studentDao.update(message); // 30ms
        return true;
    }

    public void insertOrReplaceAll(Collection<Student> message) {
        studentDao.insertOrReplaceInTx(message);
    }

    public LazyList<Student> lazyLoadmessage() {
        return studentDao.queryBuilder().listLazy();
    }

    public Student getMessageByName(String studentName) {
        return studentDao.queryBuilder()
                .where(StudentDao.Properties.StudentName.eq(studentName)).limit(1)
                .unique();
    }

    public List<Student> getMessageByStatus(String studentNum) {
        return studentDao.queryBuilder()
                .where(StudentDao.Properties.StudentNum.eq(studentNum)).list();
    }

    public List<Student> getMessageByMsgSec(String sex) {
        return studentDao.queryBuilder()
                .where(StudentDao.Properties.Sex.eq(sex)).list();
    }

    public void deleteAll() {
        studentDao.deleteAll();
    }

    public void deleteStudent(Student empty) {
        studentDao.delete(empty);
    }

    public List<Student> getMessageID(String Id) {
        return studentDao.queryBuilder().where(StudentDao.Properties.Id.eq(Id)).list();
    }
}
